package com.gzhryc.shared_device.oem.code.member.services;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.jdbc.Conditions;
import com.gzhryc.jdbc.JdbcSession;
import com.gzhryc.jdbc.MultiDBTools;
import com.gzhryc.shared_device.oem.code.member.dao.MemberDao;
import com.gzhryc.shared_device.oem.code.member.dao.db.Member;
import com.gzhryc.shared_device.oem.code.member.services.dto.MemberSearch;
import org.apache.commons.lang3.StringUtils;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

public class MemberService {

	static Logger log = Logger.getLogger(MemberService.class);

	public static MemberService self() {
		return MultiDBTools.getService(MemberService.class, true);
	}

	public static MemberService self(String jdbcKey) {
		return MultiDBTools.getService(jdbcKey, MemberService.class, true);
	}

	private String jdbcKey;

	public MemberService(String jdbcKey) {
		this.jdbcKey = jdbcKey;
	}

	public List<Member> findBySearch(MemberSearch search, Integer pageNo, Integer pageLimit) {
		if(search != null && search.getIdPrefix() != null) {
			MemberDao dao = new MemberDao(jdbcKey, search.getIdPrefix());
			Conditions condition = dao.buildConditions(search);
			try {
				return dao.findByCondition(condition, pageNo, pageLimit);
			} catch (SQLException e) {
				log.error(e.getMessage(), e);
			}
		}
		return null;
	}

	public Long countBySearch(MemberSearch search) {
		if(search != null && search.getIdPrefix() != null) {
			MemberDao dao = new MemberDao(jdbcKey, search.getIdPrefix());
			Conditions condition = dao.buildConditions(search);
			try {
				return dao.count(condition);
			} catch (SQLException e) {
				log.error(e.getMessage(), e);
			}
		}
		return 0L;
	}

	public Member getById(Long id) {
		if (id != null) {
			MemberDao dao = new MemberDao(jdbcKey, id);
			try {
				return dao.getById(id);
			} catch (SQLException e) {
				log.error(e.getMessage(), e);
			}
		}
		return null;
	}

	public Member getByPhone(String phone) {
		if (StringTools.isNotBlank(phone)) {
			MemberDao dao = new MemberDao(this.jdbcKey);

			Conditions conditions = new Conditions(Member.class);
			conditions.and().is("phone",phone);
			try {
				return dao.getBranch(conditions);
			} catch (SQLException e) {
				log.error(e.getMessage(), e);
			}
		}
		return null;
	}

	public Member getByTypeAndExId(Integer type,String appId,String openId) {
		if (type != null && StringUtils.isNotBlank(openId)) {
			MemberDao dao = new MemberDao(jdbcKey, openId);
			Conditions conditions = new Conditions(Member.class);
			conditions.and().is("type",type);
			conditions.and().is("appId",appId);
			conditions.and().is("openId",openId);
			try {
				return dao.get(conditions);
			} catch (SQLException e) {
				log.error(e.getMessage(), e);
			}
		}
		return null;
	}

	public boolean login(Long memberId) {
		if (memberId != null) {
			MemberDao dao = new MemberDao(jdbcKey, memberId);

			Member updateEntity = new Member();
			updateEntity.setId(memberId);
			updateEntity.setLastLoginDate(new Date());
			try {
				return dao.updateNotNull(updateEntity) > 0;
			} catch (SQLException e) {
				log.error(e.getMessage(), e);
			}
		}
		return false;
	}

	public Member add(Member member) throws LogicException {
		if(StringTools.isBlank(member.getOpenId()) || StringTools.isBlank(member.getAppId())){
			throw new LogicException("AppId或OpenId为空");
		}
		MemberDao dao = new MemberDao(jdbcKey, member.getOpenId());
		Long id = dao.getNewID();
		if(id == null){
			throw new LogicException("未能生成ID");
		}
		member.setId(id);
		JdbcSession.begin();
		try {
			member.setCreateDate(new Date());
			if (dao.insert(member) > 0) {
				if (MemberWalletService.self().addDefaultWallet(member.getId())) {
					JdbcSession.commit();
					return member;
				}
			}
		} catch (SQLException e) {
			log.error(e.getMessage(), e);
		}  finally {
			JdbcSession.end();
		}
		return null;
	}

	public boolean updateNotNull(Member member) throws LogicException {
		if(member != null && member.getId() != null){
			MemberDao dao = new MemberDao(jdbcKey, member.getId());
			try{
				if (dao.updateNotNull(member) > 0) {
					return true;
				}
			}catch (SQLException e){
				log.error(e.getMessage(),e);
			}
		}
		return false;
	}

	public boolean updatePhone(Long memberId,String phone){
		if(memberId != null && StringTools.isNotBlank(phone)){
			MemberDao dao = new MemberDao(jdbcKey, memberId);

			Member member = new Member();
			member.setId(memberId);
			member.setPhone(phone);

			try {
				return dao.updateNotNull(member) > 0;
			} catch (SQLException e) {
				log.error(e.getMessage(),e);
			}
		}
		return false;
	}

	public boolean deleteById(Long id) throws LogicException {
		Member member = getById(id);
		if(member == null){
			throw new LogicException("会员不存在");
		}
		MemberDao dao = new MemberDao(jdbcKey, member.getId());
		JdbcSession.begin();
		try{
			if(dao.deleteById(id) > 0){
				//删除钱包相关信息
				if(MemberWalletService.self().deleteByMemberId(id)) {
					JdbcSession.commit();
					return true;
				}
			}
		}catch (SQLException e){
			log.error(e.getMessage(),e);
		}finally {
			JdbcSession.end();
		}
		return false;
	}

}
